package com.ellricky.baselib.ui.student

import android.view.View
import android.widget.AdapterView
import androidx.recyclerview.widget.LinearLayoutManager
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.adapter.student.PersonEditAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.StaffList
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewECallback
import com.ellricky.baselib.util.NewEntityCallback
import kotlinx.android.synthetic.main.activity_student_teacher_edit.*
import java.util.*
import kotlin.collections.ArrayList

class StudentTeacherEditActivity : BaseToolBarActivity<Void, BasePresenter<Void>>(),
    AdapterView.OnItemClickListener {

    private val type: String by lazy {
        intent.getStringExtra(BaseConstant.KEY_TYPE)
    }

    private val memberId: String by lazy {
        intent.getStringExtra(BaseConstant.KEY_ID)
    }

    private val editAdapter by lazy {
        PersonEditAdapter()
    }

    override fun getContentLayout(): Int {
        return R.layout.activity_student_teacher_edit
    }

    override fun initUIView() {
        super.initUIView()
        initTitle()
        initRv()
        loadData()
    }

    private fun initTitle() {
        when (type) {
            "consultant" -> {
                mTvTitle.text = "更换课程顾问"
            }
            "teacherManager" -> {
                mTvTitle.text = "更换学管"
            }
            "teacher" -> {
                mTvTitle.text = "更换正式课老师"
            }
        }
    }

    private fun initRv() {
        rv_person.layoutManager = LinearLayoutManager(this)
        rv_person.adapter = editAdapter
        editAdapter.setOnItemClickListener(this)
    }

    private fun loadData() {
        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "headSchoolEmpList"
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance().postFrom(url, paramMap, object : NewEntityCallback<StaffList?>(this) {
            override fun onSuccess(data: StaffList?, msg: String?) {
                data?.also {
                    val list = getStaffList(it)
                    editAdapter.refresh(list)
                }
            }
        })
    }

    private fun getStaffList(staffList: StaffList): ArrayList<StaffList.Staff> {
        when (type) {
            "consultant" -> {
                return staffList.saleList
            }
            "teacherManager" -> {
                return staffList.gzaList
            }
            "teacher" -> {
                return staffList.teacherList
            }
        }
        return arrayListOf()
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        val id = editAdapter[position].id ?: 0
        modifyPerson(id)
    }

    private fun modifyPerson(id: Int) {
        val url = EAppUtil.getManagerApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        initParams(paramMap, id)
        HttpUtils.getInstance().postFrom(url, paramMap, object : NewEntityCallback<Any?>(this) {
            override fun onSuccess(data: Any?, msg: String?) {
                ToastUtil.show(this@StudentTeacherEditActivity, "分配成功")
                finish()
            }
        })
    }

    private fun initParams(paramMap: MutableMap<String, String>, id: Int) {
        paramMap["ids"] = memberId
        paramMap["eid"] = LocalCacheUtil.getUid()
        when (type) {
            "consultant" -> {
                paramMap["action"] = "allotSale"
                paramMap["sale_id"] = id.toString()
            }
            "teacherManager" -> {
                paramMap["action"] = "allotGza"
                paramMap["gza_id"] = id.toString()
            }
            "teacher" -> {
                paramMap["action"] = "allotTeacher"
                paramMap["teacher_id"] = id.toString()
            }
        }
    }
}